/*
 * Copyright (c) 2009 John Pritchard, WTKX Project Group
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wtkx.io;

import java.awt.Color;

/**
 * Color deode and encode for <code>'#rrggbb'</code> and
 * <code>'#aarrggbb'</code> and the java and html color names.
 * 
 * The named colors are available via the following strings.
 * 
 * <pre>
 * BLACK black Black
 * BLUE blue Blue
 * CYAN cyan
 * DARK_GRAY darkGray
 * GRAY gray Gray
 * GREEN green Green
 * LIGHT_GRAY lightGray
 * MAGENTA magenta
 * ORANGE orange
 * PINK pink
 * RED red Red
 * WHITE white White
 * YELLOW yellow Yellow
 * Silver silver
 * Lime lime
 * Olive olive
 * Maroon maroon
 * Navy navy
 * Purple purple
 * Teal teal
 * Fuchsia fuchsia
 * Aqua aqua
 * </pre>
 * @author jsp
 */
public class CC {

    
    /**
     * Color decode with alpha or names.
     */
    public static Color Decode(String numeric){
        int len = numeric.length();
        byte[] value = null;
        switch (numeric.charAt(0)){
        case '#':
            value = Hex.Decode(numeric.substring(1));
            break;
        case '0':
            switch (numeric.charAt(1)){
            case 'X':
            case 'x':
                value = Hex.Decode(numeric.substring(2));
                break;
            default:
                break;
            }
            break;
        default:
            break;
        }

        if (null == value){

            return Named.get(numeric);
        }
        else {
            int a = 255, r = 255, g = 255, b = 255;
            
            switch (value.length){

            case 4:
                a = (value[0] & 0xff);
                r = (value[1] & 0xff);
                g = (value[2] & 0xff);
                b = (value[3] & 0xff);
                break;

            case 3:
                r = (value[0] & 0xff);
                g = (value[1] & 0xff);
                b = (value[2] & 0xff);
                break;

            default:
                throw new IllegalArgumentException(numeric);
            }
            return new Color(r, g, b, a);
        }
    }

    public static String Encode(Color color){
        return '#'+wtkx.io.Hex.Encode(color.getRGB());
    }

    public final static Color Lookup(String name){
        return Named.get(name);
    }
    private final static java.util.Map<String,Color> Named = new java.util.HashMap<String,Color>();
    static {
        Named.put("BLACK",Color.BLACK);
        Named.put("BLUE",Color.BLUE);
        Named.put("CYAN",Color.CYAN);
        Named.put("DARK_GRAY",Color.DARK_GRAY);
        Named.put("GRAY",Color.GRAY);
        Named.put("GREEN",Color.GREEN);
        Named.put("LIGHT_GRAY",Color.LIGHT_GRAY);
        Named.put("MAGENTA",Color.MAGENTA);
        Named.put("ORANGE",Color.ORANGE);
        Named.put("PINK",Color.PINK);
        Named.put("RED",Color.RED);
        Named.put("WHITE",Color.WHITE);
        Named.put("YELLOW",Color.YELLOW);
        Named.put("black",Color.black);
        Named.put("blue",Color.blue);
        Named.put("cyan",Color.cyan);
        Named.put("darkGray",Color.darkGray);
        Named.put("gray",Color.gray);
        Named.put("green",Color.green);
        Named.put("lightGray",Color.lightGray);
        Named.put("magenta",Color.magenta);
        Named.put("orange",Color.orange);
        Named.put("pink",Color.pink);
        Named.put("red",Color.red);
        Named.put("white",Color.white);
        Named.put("yellow",Color.yellow);

        Named.put("Black",Color.BLACK);
        Named.put("Green",Color.GREEN);
        Named.put("Silver",CC.Decode("#C0C0C0"));
        Named.put("Lime",CC.Decode("#00FF00"));
        Named.put("Gray",CC.Decode("#808080"));
        Named.put("Olive",CC.Decode("#808000"));
        Named.put("White",Color.WHITE);
        Named.put("Yellow",Color.YELLOW);
        Named.put("Maroon",CC.Decode("#800000"));
        Named.put("Navy",CC.Decode("#000080"));
        Named.put("Peal",CC.Decode("#c0102080"));
        Named.put("Red",Color.RED);
        Named.put("Blue",Color.BLUE);
        Named.put("Purple",CC.Decode("#800080"));
        Named.put("Teal",CC.Decode("#008080"));
        Named.put("Fuchsia",CC.Decode("#FF00FF"));
        Named.put("Aqua",CC.Decode("#00FFFF"));
        Named.put("Ultra",CC.Decode("#a0efefef"));
        Named.put("Sele",CC.Decode("#a0e0e005"));

        Named.put("silver",CC.Lookup("Silver"));
        Named.put("lime",CC.Lookup("Lime"));
        Named.put("olive",CC.Lookup("Olive"));
        Named.put("maroon",CC.Lookup("Maroon"));
        Named.put("navy",CC.Lookup("Navy"));
        Named.put("peal",CC.Lookup("Peal"));
        Named.put("purple",CC.Lookup("Purple"));
        Named.put("teal",CC.Lookup("Teal"));
        Named.put("fuchsia",CC.Lookup("Fuchsia"));
        Named.put("aqua",CC.Lookup("Aqua"));
        Named.put("ultra",CC.Lookup("Ultra"));
        Named.put("sele",CC.Lookup("Sele"));
    }

}
